package com.googlecode.wannabet.controller.ui;

import java.util.Date;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.googlecode.wannabet.domain.Match;

public class CreateMatchController extends Controller {

	@Override
	protected Navigation run() throws Exception {
		String submit = asString("submit");
		if(!"true".equals(submit)) {
			return forward("/jsps/createMatch.jsp");
		}
		
		Long id = asLong("matchID");
		Long tournamentID = asLong("tournamentID");
		Date eventDate = asDate("eventDate", "dd/MM/yyyy");
		Long venueID = asLong("venueID");
		Long team1 = asLong("team1");
		Long team2 = asLong("team2");
		
		Entity match = Match.getMatchEntity(id, tournamentID, eventDate, venueID, team1, team2);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(match);
		
		return redirect("/ui/");
	}

}
